package com.qumitech.visualnovel.component.util;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;



/**
 * @Author :  ZhChNing
 * @Descriptiom :
 * @Date Created in 2020/1/10 14:42
 */

@Component
public class LocationUtil {
    private static final int success = 0;
    private static final String ak = GetPropertiesUtils.getProperty("common", "baidu_ak");

    private final Logger Log = LoggerFactory.getLogger(this.getClass());


    public String getLocationAddressByIP(String ip) {
        String url = "https://api.map.baidu.com/location/ip?ak=" + ak + "&ip=" + ip+"&coor=bd09ll";
        Log.info(url);
        String rsp;
        try {
            rsp = HttpInvokeUtil.httpGet(url, new HashMap<>());
            JSONObject o = JSONObject.parseObject(rsp);

            Log.info("BaiduIpLocationDTO  ip:{},rsp:{}", ip, rsp);
            if (o.getInteger("status").equals(0)) {
                return o.getJSONObject("content").getString("address");
            }
        } catch (Exception e) {
            return "";
        }

        return "";
    }


    public static void main(String[] args) {

        String ip = "123.185.105.239";
        System.out.println(JSON.toJSONString(new LocationUtil().getLocationAddressByIP(ip)));

    }

}
